package com.shujia.flink.source

import org.apache.flink.streaming.api.scala._
import org.apache.flink.api.common.eventtime.WatermarkStrategy
import org.apache.flink.api.common.serialization.SimpleStringSchema
import org.apache.flink.connector.kafka.source.KafkaSource
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer

object Demo4KafkaSource {
  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    /**
     * 基于kafka构建DataStream
     */

    val source: KafkaSource[String] = KafkaSource
      .builder[String]
      .setBootstrapServers("master:9092,node1:9092,node2:9092") //kafka集群列表
      .setTopics("student_json") //topic
      .setGroupId("my-group") //消费者组
      .setStartingOffsets(OffsetsInitializer.earliest()) //读取数据的位置
      .setValueOnlyDeserializer(new SimpleStringSchema()) //反序列化类
      .build

    //构建DataStream
    val kafkaSource: DataStream[String] = env.fromSource(source, WatermarkStrategy.noWatermarks(), "Kafka Source")

    kafkaSource.print()

    env.execute()

  }

}
